﻿using Business;
using Model;
using Public;
using Public.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using wochu.DPS.API.Infrastructure;

namespace wochu.DPS.API.Controllers
{
    public class GoodsinventoryController : ApiController
    {
        private static BGoods _NewInstance = new BGoods();

        public static BGoods NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new BGoods();
                }

                return _NewInstance;
            }


        }

        [HttpGet]
        public HttpResponseMessage GetPftgoodsinventory(string locationcode)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(locationcode))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                var data = _NewInstance.Getgoods(locationcode);
                if (data == null || data.Count <= 0)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无关联商品";
                    return ResultsExtensions.GetFailJson(sysSet);
               
                }
                
                return ResultsExtensions.GetSuccessJson(data);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }
        }

        [HttpPost]
        public HttpResponseMessage Loss_Pftgoodsinventory(List<PFTGOODSinventoryModel.GetGoodsModel> UpdateGoodsinventory, string UnShelveType)
        {
            string message = "";
            SysSetting sysSet = new SysSetting();
            if (UpdateGoodsinventory.Count <= 0 || UpdateGoodsinventory == null || UnShelveType == null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            
            }
            if (NewInstance.UpdatePft_goodsinventory(out message, UpdateGoodsinventory, UnShelveType))
                return ResultsExtensions.GetSuccessJson(true);
            else
            {
                SysSetting syserr = new SysSetting() { Key = "0", Value = message };
                return ResultsExtensions.GetFailJson(syserr);
            }
        }

        [HttpPost]
        public HttpResponseMessage PFT_UpdateGoodsinventory(List<PFTGOODSinventoryModel.PFT_UpdateGoodsinventory> List_UpdateGoodsinventory)
        {
            string message = "";
            SysSetting sysSet = new SysSetting();
            if (List_UpdateGoodsinventory.Count <= 0 || List_UpdateGoodsinventory == null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);

            }
            if (NewInstance.PFT_UpdateGoodsinventory(out message, List_UpdateGoodsinventory))
                return ResultsExtensions.GetSuccessJson(true);
            else
            {
                SysSetting syserr = new SysSetting() { Key = "0", Value = message };
                return ResultsExtensions.GetFailJson(syserr);
            }
        }

        /// <summary>
        /// 现接口获取的是摘果的库存
        /// </summary>
        /// <param name="Goodscode"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetPftMiddlegoodsinventory(string Goodscode)
        {
            SysSetting sysSet = new SysSetting();
            if (string.IsNullOrEmpty(Goodscode))
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);
            }
            try
            {
                var data = _NewInstance.GetMiddlegoodsInventory(Goodscode);
                if (data == null)
                {
                    sysSet.Key = Consts.strFalse;
                    sysSet.Value = "无摘果仓库存商品";
                    return ResultsExtensions.GetFailJson(sysSet);

                }

                return ResultsExtensions.GetSuccessJson(data);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                sysSet.Key = Consts.strFalse;
                sysSet.Value = " 获取信息失败，请查看日志";
                return ResultsExtensions.GetFailJson(sysSet);
            }

        }

        /// <summary>
        /// 摘果仓库存修改
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="List_UpdateMiddleGoodsinventory"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage PFT_UpdateMiddleGoodsinventory(int UserID,List<pft_middle_inventoryTMP> List_UpdateMiddleGoodsinventory)
        {
            string message = "";
            SysSetting sysSet = new SysSetting();
            if (List_UpdateMiddleGoodsinventory.Count <= 0 || List_UpdateMiddleGoodsinventory == null)
            {
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "参数为NULL";
                return ResultsExtensions.GetFailJson(sysSet);

            }
            if (NewInstance.PFT_UpdateMiddleGoodsinventory(out message, UserID, List_UpdateMiddleGoodsinventory))
                return ResultsExtensions.GetSuccessJson(true);
            else
            {
                SysSetting syserr = new SysSetting() { Key = "0", Value = message };
                return ResultsExtensions.GetFailJson(syserr);
            }
        }

    }
}
